package com.web.controller.web;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.web.dao.*;
import com.web.domain.WebResponse;
import com.web.domain.dataTable.BootstrapTableColumns;
import com.web.domain.dataTable.BootstrapTableResponse;
import com.web.entity.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author FDjavaone
 * @date 2017/9/28
 */
@Slf4j
@Controller
public class WebHomeController {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private PlatformMapper platformMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private StoreChangeMapper storeChangeMapper;
    @Autowired
    private WithdrawApplyMapper withdrawApplyMapper;
    @Autowired
    private StoreApplyMapper storeApplyMapper;
    @Autowired
    private AgentWithdrawApplyMapper agentWithdrawApplyMapper;
    @Autowired
    private RegionRecordMapper regionRecordMapper;
    @Autowired
    private MskPriceCountMapper mskPriceCountMapper;
    @Autowired
    private CompanyBonusDetailMapper companyBonusDetailMapper;
    @Autowired
    private CompanyCompanyMsDetailMapper companyCompanyMsDetailMapper;
    @Autowired
    private CompanyFundDetailMapper companyFundDetailMapper;
    @Autowired
    private CompanyMarketDetailMapper companyMarketDetailMapper;
    @Autowired
    private CompanyProfitDetailMapper companyProfitDetailMapper;
    @Autowired
    private CompanyMarketMsDetailMapper companyMarketMsDetailMapper;


    @RequestMapping(value = "home",
            method = RequestMethod.GET)
    public String store(Model model, HttpServletRequest request) {
        return "home";
    }


    @Data
    public static class IndexDto extends Platform {
        private BigDecimal todayNum;
        private BigDecimal yesterdayNum;
        private BigDecimal platformNum;
        private BigDecimal merchantNum;
        private Platform platform;
        private Integer commonNum;
        private Integer originalNum;
        private Integer storeOnline;
        private Integer storeOutline;
        private Integer agentNum;
        private BigDecimal coreNum;
        private BigDecimal consumeNum;
        private BigDecimal oriNum;
        private BigDecimal profitNum;
        private int storeApply;
        private int storeChange;
        private int withdraw;
        private int agentWithdraw;

    }

    @RequestMapping("homeCount")
    @ResponseBody
    public WebResponse homeCount() {
        int storeApply = storeApplyMapper.countWaiting();
        int storeChange = storeChangeMapper.countWaiting();
        int withdraw = withdrawApplyMapper.countWaiting();
        int agentWithdraw = agentWithdrawApplyMapper.countWaiting();

        BigDecimal todayNum = orderMapper.countTodayNum();
        BigDecimal yesterdayNum = orderMapper.countYesterdayNum();
        BigDecimal platformNum = orderMapper.countTotalNum();
        BigDecimal merchantNum = accountMapper.countMerchantNum();
        Platform platform = platformMapper.selectByPrimaryKey(1L);
        Integer commonNum = memberMapper.countCommon();
        Integer originalNum = memberMapper.countOri();
        Integer storeOnline = storeMapper.countOnline();
        Integer storeTotal = storeMapper.countTotal();
        Integer storeOutline = storeTotal - storeOnline;
        Integer agentNum = regionRecordMapper.countTotal();
        BigDecimal coreNum = accountMapper.countCoreNum();
        BigDecimal consumeNum = accountMapper.countConsumeNum();
        BigDecimal oriNum = accountMapper.countOriNum();
        BigDecimal profitNum = accountMapper.countProfitNum();
        IndexDto dto = new IndexDto();
        dto.setTodayNum(todayNum);
        dto.setYesterdayNum(yesterdayNum);
        dto.setPlatformNum(platformNum);
        dto.setMerchantNum(merchantNum);
        dto.setCommonNum(commonNum);
        dto.setOriginalNum(originalNum);
        dto.setStoreOnline(storeOnline);
        dto.setStoreOutline(storeOutline);
        dto.setAgentNum(agentNum);
        dto.setCoreNum(coreNum);
        dto.setConsumeNum(consumeNum);
        dto.setOriNum(oriNum);
        dto.setProfitNum(profitNum);
        dto.setPlatform(platform);
        dto.setStoreApply(storeApply);
        dto.setStoreChange(storeChange);
        dto.setWithdraw(withdraw);
        dto.setAgentWithdraw(agentWithdraw);

        return WebResponse.ok("获取成功", dto);
    }


    @RequestMapping("MSKPrice")
    @ResponseBody
    public WebResponse MSKPrice() {
        List<MskPriceCount> mskPriceCounts = mskPriceCountMapper.selectNewSixty();
        Collections.sort(mskPriceCounts, new Comparator<MskPriceCount>() {
            @Override
            public int compare(MskPriceCount o1, MskPriceCount o2) {
                int i = new Integer(String.valueOf(o1.getId() - o2.getId()));
                return i;
            }
        });
        return WebResponse.ok("获取成功", mskPriceCounts);
    }


    @Data
    public static class DetailsColumns extends BootstrapTableColumns {

    }

    @RequestMapping("/sys/companyMs/getDetail")
    @ResponseBody
    public BootstrapTableResponse companyMs(DetailsColumns detailsColumns) {
        PageHelper.startPage(detailsColumns.getPageNumber(), detailsColumns.getPageSize());
        Page<CompanyCompanyMsDetail> page = companyCompanyMsDetailMapper.selectRecord();
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }


    @RequestMapping("/sys/marketMs/getDetail")
    @ResponseBody
    public BootstrapTableResponse marketMs(DetailsColumns detailsColumns) {
        PageHelper.startPage(detailsColumns.getPageNumber(), detailsColumns.getPageSize());

        Page<CompanyMarketMsDetail> page = companyMarketMsDetailMapper.selectRecord();
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }


    @RequestMapping("/sys/companyNum/getDetail")
    @ResponseBody
    public BootstrapTableResponse companyNum(DetailsColumns detailsColumns) {
        PageHelper.startPage(detailsColumns.getPageNumber(), detailsColumns.getPageSize());
        Page<CompanyProfitDetail> page = companyProfitDetailMapper.selectRecord();
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }


    @RequestMapping("/sys/companyFund/getDetail")
    @ResponseBody
    public BootstrapTableResponse companyFund(DetailsColumns detailsColumns) {
        PageHelper.startPage(detailsColumns.getPageNumber(), detailsColumns.getPageSize());
        Page<CompanyFundDetail> page = companyFundDetailMapper.selectRecord();
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }


    @RequestMapping("/sys/marketFund/getDetail")
    @ResponseBody
    public BootstrapTableResponse marketFund(DetailsColumns detailsColumns) {
        PageHelper.startPage(detailsColumns.getPageNumber(), detailsColumns.getPageSize());
        Page<CompanyMarketDetail> page = companyMarketDetailMapper.selectRecord();
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }

    @RequestMapping("/sys/bonusFund/getDetail")
    @ResponseBody
    public BootstrapTableResponse bonusFund(DetailsColumns detailsColumns) {
        PageHelper.startPage(detailsColumns.getPageNumber(), detailsColumns.getPageSize());
        Page<CompanyBonusDetail> page = companyBonusDetailMapper.selectRecord();
        BootstrapTableResponse bootstrapTableResponse = new BootstrapTableResponse();
        bootstrapTableResponse.setResult(page.getResult());
        bootstrapTableResponse.setTotalCount(page.getTotal());
        return bootstrapTableResponse;
    }
}
